package org.minuframework.file;

import java.io.File;
import java.util.List;

import com.google.common.collect.Lists;

public class Directory {
    
    /**
     * 文件夹
     */
    private String parent;
    
    /**
     * 文件名
     */
    private String name;
    
    /**
     * 文件列表
     */
    private List<String> files;
    
    /**
     * 文件目录
     */
    private List<Directory> directories;
    
    public Directory() {
        this(null, null);
    }
    
    public Directory(String parent) {
        this(parent, null);
    }
    
    public Directory(String parent, String name) {
        this.parent = parent;
        this.name = name;
    }
    
    public String getParent() {
        return parent;
    }
    
    public File getParentFile() {
        return new File(parent);
    }
    
    public void setParent(String parent) {
        this.parent = parent;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public List<String> getFiles() {
        return (files == null ? (files = Lists.newArrayList()) : files);
    }
    
    public void setFiles(List<String> files) {
        this.files = files;
    }
    
    public List<Directory> getDirectories() {
        return (directories == null ? (directories = Lists.newArrayList()) : directories);
    }
    
    public List<Directory> getDirectories(File file) {
        if (!Files.isDirectory(file)) { return getDirectories(); }
        List<Directory> result = Lists.newArrayList();
        for (Directory directory : getDirectories()) {
            String parent = directory.getParent();
            if (parent.equals(file.toString())) {
                result.add(directory);
                continue;
            }
            if (file.toString().indexOf(parent) != -1) {
                result.addAll(getDirectories(directory, file));
            }
        }
        return result;
    }
    
    private List<Directory> getDirectories(Directory directory, File file) {
        List<Directory> result = Lists.newArrayList();
        for (Directory d : directory.getDirectories()) {
            String parent = d.getParent();
            if (parent.equals(file.toString())) {
                result.add(d);
                continue;
            }
            if (file.toString().indexOf(parent) != -1) {
                result.addAll(getDirectories(d, file));
            }
        }
        return result;
    }
    
    public void setDirectories(List<Directory> directories) {
        this.directories = directories;
    }
    
    public int getDirectoriesSize() {
        return getDirectories().size();
    }
    
    public int getFilesSize() {
        return getFiles().size();
    }
    
}
